﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;

namespace blackhouse.BlackApart.Data.Caching {
    public class CachedApartmentDataAccess : IApartmentDataAccess {

        #region Fields

        private IApartmentDataAccess decoratedDataAccess;
        private NemoCache cache;

        #endregion

        #region Constructors

        public CachedApartmentDataAccess(IApartmentDataAccess decoratedDataAccess, string connectionStringToDecoretedDb) {
            this.decoratedDataAccess = decoratedDataAccess;
            this.cache = new NemoCache(TimeSpan.FromMinutes(2), connectionStringToDecoretedDb);
        }

        #endregion

        #region Methods

        public int SaveApartment(int id, string name, int groupId, int locationId, int numberOfPeople, float areaSize) {
            this.cache.TouchTimes();
            return this.decoratedDataAccess.SaveApartment(id, name, groupId, locationId, numberOfPeople, areaSize);
        }

        public ApartmentData GetApartment(int apartmentId) {
            cache.Reset();
            cache.SetDependencyTables("T19", "T28");
            cache.SetParameters(apartmentId);
            cache.SetKey("GetApartment");
            ApartmentData data = cache.GetData<ApartmentData>();
            if (data.Id == 0) {
                data = this.decoratedDataAccess.GetApartment(apartmentId);
                cache.SetCaching(data);
            }
            return data;
        }

        public ApartmentData[] GetFreeApartments(DateTime startDate, DateTime endDate, int apartmentGroupId) {
            cache.Reset();
            cache.SetDependencyTables("T19", "T28", "T43");
            cache.SetParameters(startDate, endDate, apartmentGroupId);
            cache.SetKey("GetFreeApartments");
            ApartmentData[] data = cache.GetData<ApartmentData[]>();
            if (data == null) {
                data = this.decoratedDataAccess.GetFreeApartments(startDate, endDate, apartmentGroupId);
                cache.SetCaching(data);
            }
            return data;
        }

        public ApartmentData[] GetApartmentsByGroup(int groupId) {
            cache.Reset();
            cache.SetDependencyTables("T19", "T28");
            cache.SetParameters(groupId);
            cache.SetKey("GetApartmentsByGroup");
            ApartmentData[] data = cache.GetData<ApartmentData[]>();
            if (data == null) {
                data = this.decoratedDataAccess.GetApartmentsByGroup(groupId);
                cache.SetCaching(data);
            }
            return data;
        }

        public ApartmentPhotoData[] GetApartmentPhotos(int apartmentId) {
            cache.Reset();
            cache.SetDependencyTables("T19", "T105", "T109", "T28_T105");
            cache.SetParameters(apartmentId);
            cache.SetKey("GetApartmentPhotos");
            ApartmentPhotoData[] data = cache.GetData<ApartmentPhotoData[]>();
            if (data == null) {
                data = this.decoratedDataAccess.GetApartmentPhotos(apartmentId);
                cache.SetCaching(data);
            }
            return data;
        }

        public ApartmentPhotoData[] GetApartmentTypePhotos(int apartmentTypeId, string language) {
            cache.Reset();
            cache.SetDependencyTables("T105", "T109", "T28_T105");
            cache.SetParameters(apartmentTypeId);
            cache.SetKey("GetApartmentTypePhotos" + language);
            ApartmentPhotoData[] data = cache.GetData<ApartmentPhotoData[]>();
            if (data == null) {
                data = this.decoratedDataAccess.GetApartmentTypePhotos(apartmentTypeId, language);
                cache.SetCaching(data);
            }
            return data;
        }

        #endregion

    }
}
